package com.study.spmongodb.wwj.juc.utils.executors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/15 22:23
 * https://www.bilibili.com/video/BV1hJ411D7k2?p=136
 */
public class ExecutorsExample1 {

    public static void main(String[] args) throws InterruptedException {

        useCache();

    }

    private static void useCache() throws InterruptedException {
        final ExecutorService threadPool = Executors.newCachedThreadPool();
        IntStream.range(0, 100).boxed().forEach(i -> threadPool.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " finished.");
        }));
        TimeUnit.SECONDS.sleep(10);
    }
}
